#include <Windows.h>

#include <GL/gl.h>
#include <GL/glu.h>

#include "GLWindow.h"
#include "Example.h"

#include <string.h>
#include "MeshLoaader.h"

ObjMesh g_tank;

Example::Example(void)
{
	m_rotationAngle = 0.0f;

	g_tank = MeshLoaader::LoadObjMesh("tank.obj");
}

bool Example::init()
{
	glEnable(GL_DEPTH_TEST);
	glClearColor(0.5f, 0.5f, 0.5f, 0.5f);

	return true;
}

void Example::update( float i_fDeltaTime )
{
	const float SPEED = 15.0f;
	m_rotationAngle += SPEED * i_fDeltaTime;
	if (m_rotationAngle > 360.0f)
	{
		m_rotationAngle -= 360.0f;
	}
}

void Example::render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glRotatef(m_rotationAngle, 0.0f, 0.0f, 1.0f);
/*
	glEnableClientState(GL_VERTEX_ARRAY); //Enable the vertex array
	//Tell OpenGL where the vertices are
	glVertexPointer(3, GL_FLOAT, 0, &g_tank.faces[0].vertices.[0]);
	//Draw the triangle, starting from vertex index zero
	glDrawArrays(GL_TRIANGLES, 0, 3);
	//Finally disable the vertex array
	glDisableClientState(GL_VERTEX_ARRAY);
*/
/*	
	glBegin(GL_TRIANGLES);
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex3f(-0.5f, -0.5f, -2.0f);
		glColor3f(0.0f, 1.0f, 0.0f);
		glVertex3f( 0.5f, -0.5f, -2.0f);
		glColor3f(0.0f, 0.0f, 1.0f);
		glVertex3f( 0.0f, 0.5f, -2.0f);
	glEnd();
*/


	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(0,400, 400, 0);
	
	// Viewing transformation
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	// Blue 3D Text - Note color is set before the raster position
	glColor3f(1.0f, 1.0f, 1.0f);
	glRasterPos2i(3, 10);
	glListBase(GLWindow::getSystemFont());
	glCallLists ( 13, GL_UNSIGNED_BYTE, "BaseGL - Test" );

	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}

void Example::shutdown()
{

}

void Example::onResize( int width, int height )
{
	glViewport(0, 0, width, height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	gluPerspective(45.0f, float(width) / float(height), 1.0f, 100.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

bool Example::destroy()
{
	return true;
}